/*  iImager (network disk imaging via iscsi)
	Copyright (C) 2009-2010 Yao Wei <njustyw@gmail.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


#ifndef HDDUPLICATOR_H
#define HDDUPLICATOR_H

#include "Q_Singleton.h"
#include "trimps_forensic_global.h"

class HDObject;
class ImagerTask;
class ISCSIFinder;

class HDDuplicator : public QObject, public QSingleton<HDDuplicator>
{
    Q_OBJECT
	friend class QSingleton<HDDuplicator>;

public:
	IRESULT connectRack(const QString& ip);
	IRESULT disconnectRack(const QString& ip);
	IRESULT	getWholeStatusORack(const QString& ip, RackStatus& result);
	IRESULT getDetailsOfDisk(const QString& ip, int location, DiskDetails& result);
	IRESULT getDetailsOfImagingTask(QList<ImagingTaskStatus>& results);
	IRESULT newImagingTask(const QString& ip, int location, const QString& caseno, int imagingType, int hashType);
	IRESULT pauseImagingTask(const QString& ip, int location);
	IRESULT continueImagingTask(const QString& ip, int location);
	IRESULT cancelImagingTask(const QString& ip, int location);

	/** Connect to Imager daemon service */
	void connectToDaemonService();

private slots:
	/** What will be done when iSCSI iSCSIDiscovery service daemon is connected*/
	void updateServiceStatus(bool flag);
	
	/** What will be done when one rack machine is connected/disconnected*/
	void updateRackStatus(const QString& ip, bool flag);
	
	/** Add new disk(s) */
	void addISCSIDisks(const QStringList &ipList, const QStringList& deviceList, const QStringList& targetList);
	
	/** Remove nonexist disk(s) */
	void removeISCSIDisks(const QStringList &ipList, const QStringList& deviceList, const QStringList& targetList);

private:
    HDDuplicator(QObject* object = 0);
	~HDDuplicator();

	/** Allocate memory of 12 HDObjects for one rack machine*/
	void allocateForRack(const QString& ip);
	
	ISCSIFinder* iSCSIDiscovery;
	ImagerTask* taskManage;
	
	QMutex mutex;

	QHash<QString, int> rackStatus;
	QHash<QString, QList<HDObject*> > AllDisks;

	bool serviceStatus;
	QStringList liveIP;
	int rackIndex;

	friend class ImagerTask;
};

#endif // HDDUPLICATOR_H

